<template>
  <div>
    <el-card>
      <el-form ref="query" :model="query" :inline="true" label-width="80px">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="请输入内容" class="input-with-select">
            <el-button slot="append" icon="el-icon-search" />
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="add">上传</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="dataList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#" />
        <el-table-column prop="VideoId" label="视频id" width="180" />
        <el-table-column prop="CoverURL" label="视频封面">
          <template slot-scope="scope">
            <div class="video_img">
              <img :src="scope.row.CoverURL">
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="Title" label="视频标题" />
        <el-table-column prop="CreateTime" label="创建时间" />
        <el-table-column prop="Status" label="状态" />
        <el-table-column prop="Size" label="大小">
          <template slot-scope="scope">
            <div>{{ (scope.row.Size / 1048576).toFixed(2) }}M</div>
          </template>
        </el-table-column>
        <el-table-column>
          <template slot-scope="scope">
            <el-button
              type="danger"
              @click="delVideo(scope.row.VideoId)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="query.page"
        :page-size="query.pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <el-dialog :visible.sync="aliyunUploadVisible">
      <AliyunUpload @uploadSuccess="uploadSuccess" />
    </el-dialog>
  </div>
</template>

<script>
import AliyunUpload from '@/components/AliyunUpload/index'
import { getAliyunVideoList, deleteAliyunVideo } from '@/api/aliyun/video'
export default {
  components: {
    AliyunUpload
  },
  data() {
    return {
      aliyunUploadVisible: false,

      dataList: [],
      total: 0,
      query: {
        page: 1,
        pageSize: 10,
        keyword: ''
      }
    }
  },
  created() {
    this.getVideoList()
  },
  methods: {
    async getVideoList() {
      const { data: res } = await getAliyunVideoList(this.query)
      this.dataList = res.videoList.Video
      console.log(this.dataList, 'dataList')
      this.total = res.total
    },
    handleCurrentChange(page) {
      this.query.page = page
      this.getVideoList()
    },
    add() {
      this.aliyunUploadVisible = true
    },
    uploadSuccess: function(info) {
      console.log(info, 'info')
      this.aliyunUploadVisible = false
      this.getVideoList()
    },

    async delVideo(videoId) {
      this.$confirm('是否确认删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await deleteAliyunVideo(videoId)
          if (res.code === 1) {
            this.getVideoList()
            this.$message.success(res.msg)
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(res => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style scoped>
.video_img img {
  width: 150px;
}
</style>
